<?php

/*
  Project: Blogmer - Open Source Blogging Software
  Type: PHP
  Desc: Contains all functions related to comments.
  Author: Yashpal
  Usage:  	$this->load->model('Comments_model','',TRUE);
			

  Function name : 
  ReadComments($URLTitle) -> This function read all comments for a given post using URLTitle
  SaveComment()           -> This function save a comment(s) for given post.
  ReadComments($number)	  -> This function Give recent given number of comments.

???????????????????????????????????????????????
missing features
	counting of comments are remaining [done]
	admin color
	comment date
	comments id is missing [difficult to navigate]
????????????????????????????????????????????????	

*/


class Comments_model extends Model {

		
    function Comments_model()
    {
        // Call the Model constructor
        parent::Model();
	}
	
	//Function Name - ReadComments($URLTitle)
	//This function read all comments for a given post using URLTitle
	//Parram - $URLTitle 
	//output - All comments for given urltitle.
	function ReadComments($URLTitle)
	{
		/*CommentID MemberName MemberID GuestName GuestEmail GuestURL UrlTitle Content */	

			
		  $strr='';
		  $this->db->where(array('URLTitle'=>$URLTitle));
		  $query = $this->db->get('comments');
		  //$posts = $query->result_array(); 
		  if($query->num_rows()>0)
		  {
		   $strr.='<a name="comments" id="comments"></a>
		   		   <div id="user-comments">
				   		<h2>User Comments</h2>'; 
			
		  foreach ($query->result() as $row)
		 {   
		 	$date= anchor('blog/post/'.$row->URLTitle.'#comments'.$row->CommentID,date("l, F jS, Y \\a\\t n:i A",strtotime($row->CommentDate)));				
		   
		 	if($row->MemberName=="")
			{
				//guest comment
				
				if(trim($row->GuestURL)=="")
				{//if no url found insert name only 
					$link=$row->GuestName;
				}
				else
				{//insert link
					$link=anchor($row->GuestURL,$row->GuestName);
				}
				
				$strr.='<a name="comments'.$row->CommentID.'" id="comments'.$row->CommentID.'"></a>
						<div class="guest"><p>'.$link.' on '.$date.'</p>
						<p>'.$row->Content.'</p></div>';
			}
			else
			{
			//Member Section - Extract All Information
			$this->db->where('MemberID',$row->MemberID);
			$query=$this->db->get('users');
			$member=$query->row_array();
			
						
			
				//member comment
				if($member['MUrl']=="")
				{//insert nothing
					$link=$row->MemberName;
				}
				else
				{//insert link
					$link=anchor($member['MUrl'],$member['MFirstName']." ".$member['MLastName']);
				}
				$strr.='<a name="comments'.$row->CommentID.'" id="comments'.$row->CommentID.'"></a>
						<div class="member"><p>'.$link.' on '.$date.' - '.anchor('users/ViewProfile/'.$member['MemberID'],'View Profile').'</p>
						<p>'.$row->Content.'</div></p>';
			}
			
		  
		 }	
		 $strr.='</div>';
		 }
		
				
		  return $strr;
		 
	}
	//ReadComments($URLTitle)  - Ends here

	//Function Name - SaveComment()
	//This function save a comment(s) for given post.
	//Parram - no input
	//output - no output
	function SaveComment()
	{
	
		$login = $this->session->userdata('logged_in');
		if($login)
		{
			$mname=$this->input->post('name');
			$user_id=$this->input->post('user_id');
			$gname="";
			$gemail="";
		}
		else
		{
			$gname=$this->input->post('name');
			$gemail=$this->input->post('email');
			$user_id="";
			$mname="";
		}
		
		$post= array(
						'CommentID'=>"",
						'MemberName' => $mname,
						'MemberID'=>$user_id,
						'GuestName' => $gname,
						'GuestEmail' => $gemail,
						'GuestURL' => prep_url($this->input->post('guesturl')),
						'URLTitle'=>$this->input->post('urltitle'),
						'Content'  => $this->input->post('commentbox'),
						'CommentDate'=>date('Y-m-d H:i:s')
				 	);	
		

		$result=$this->db->insert('comments', $post);
		
    	$this->db->where('URLTitle', $this->input->post('urltitle'));
	    $query =$this->db->get('posts');
		$posts = $query->row_array();
		$m=(int)$posts['TotalComments']+1;
		$data = array('TotalComments' => $m);

	    $this->db->where('URLTitle', $this->input->post('urltitle'));
    	$this->db->update('posts', $data); 
    	
		redirect('/blog/post/'.$this->input->post('urltitle'));
	}
	
	
	//****************************************
	//Admin Functions
	//****************************************
	
	
	function ListAllComments($pageid=1)
	{
			$this->db->from('comments');
			$cnt=$this->db->count_all_results();
			
			$config['base_url'] = site_url().'/admin/ManageComments/';
			$config['total_rows'] =$cnt;
			$config['per_page'] = '10';
			$config['num_links'] = '3';
			$config['uri_segment'] = '3';
	
	    $pageid=self::RefinePagNav($pageid);
			$this->pagination->initialize($config); 
//			$this->db->order_by("CatID", "desc"); 
			$query = $this->db->get('comments',$config['per_page'],$this->uri->segment(3));
			
			$out= "<div id=\"table-lines\"><table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\" >";
	$out.="<caption>Comments List</caption>
			<thead>
				<tr>
					<th scope=\"col\">Comment ID</th>
					<th scope=\"col\">Posted In</th>

					<th scope=\"col\">Comment</th>
					<th scope=\"col\">Edit</th>
					<th scope=\"col\">Delete</th>
				</tr>
			</thead>	
			<tfoot>
				<tr>
					<th scope=\"row\">Total</th>

					<td colspan=\"4\">$cnt Comments</td>
				</tr>
			</tfoot>
			<tbody>";
			foreach ($query->result() as $row)
			{
				$out.=
				 "<tr>
					<td>".$row->CommentID."</td>
					<td>".$row->URLTitle."</td>
					<td>".substr($row->Content,0,200)."</td>
					<td>".anchor('admin/EditComment/'.$row->CommentID.'/'.$pageid,'Edit')."</td>
					<td>".anchor('admin/DeleteComment/'.$row->CommentID.'/'.$pageid,'Delete')."</td>
				  </tr>";
				
				
			} 
			$out.= "</tbody></table></div>";
			$out.= $this->pagination->create_links();
			return $out;
			
	}
	//end listallcomment
	function UpdateComment($comment_id,$page_id)
		{
		/*	CatID Cat RSS CatPrivate ParentID*/
		
			
		if($this->input->post('guest')==false)
		{
			$mname=$this->input->post('name');
			$user_id=$this->input->post('user_id');
			$gname="";
			$gemail="";
		}
		else
		{
			$gname=$this->input->post('name');
			$gemail=$this->input->post('email');
			$user_id="";
			$mname="";
		}

			$post= array(
						'MemberName' => $mname,
						'MemberID'=>$user_id,
						'GuestName' => $gname,
						'GuestEmail' => $gemail,
						'GuestURL' => $this->input->post('guesturl'),
						'URLTitle'=>$this->input->post('urltitle'),
						'Content'  => $this->input->post('commentbox')
				 	);	
		
			$this->db->where(array('CommentID'=>$comment_id));
			$result=$this->db->update('comments', $post);
			//redirect('/admin/ManageCategories/'.$pageid,'refresh');
			
			if($result)
				$data['info']="comment updated successfully";
			else
				$data['info']="comment updation failed";
			
			$page_id=self::RefinePagNav($page_id);
			$data['title']="Information Page";
			$data['header']="Information";
			$data['url']='/admin/ManageComments/'.$page_id;
			$data['time']="1";
			
					
			$this->load->view('admin/info',$data);
		}
		
		function DeleteComment($comment_id,$page_id)
		{
			$page_id=self::RefinePagNav($page_id);
      $this->db->where('CommentID', $comment_id);
			$result=$query=$this->db->delete('comments');
			//redirect('/admin/ManageCategories/'.$pageid, 'refresh');
			if($result)
				$data['info']="comment deleted successfully";
			else
				$data['info']="comment deletion failed";
			
			$data['title']="Information Page";
			$data['header']="Information";
			$data['url']='/admin/ManageComments/'.$page_id;
			$data['time']="1";
			
					
			$this->load->view('admin/info',$data);
		}
		
		function EditLoadComment($CommentID)
		{
			  $this->db->where('CommentID',$CommentID);
			  $query = $this->db->get('comments');
			  $posts = $query->row_array(); 
			  					
			  return $posts;
			 
		}
		
		function RefinePagNav($page_id)
		{
			if($page_id==1)
			$page_id=0;
			
			return $page_id;
		}
		
		
	

}


